//
//  ContactViewController.m
//  EWork
//
//  Created by administrator on 14-11-28.
//  Copyright (c) 2014年 gaobo. All rights reserved.
//

#import "ContactViewController.h"
#import "Contact.h"
#import "ContactCell.h"
#import "ContactDetailViewController.h"

@class ContactDetailViewController;

@interface ContactViewController ()
@property (nonatomic,retain) NSMutableArray *contactArray;
@property (nonatomic,retain) ContactCell *cell;
@property(retain)Contact *contact;
@end

@implementation ContactViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    /*
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *filePath= [NSString stringWithFormat:@"%@/contact.plist",docDir];
    NSLog(@"%@",filePath);
    NSMutableArray *tempArray=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    if (tempArray==nil) {
        self.contactArray=[NSMutableArray array];
    }else{
        self.contactArray=[NSMutableArray arrayWithArray:tempArray];
    }
     */

    Contact *contact1 = [[Contact alloc]initWithId:@"2" Name:@"张1" HeadImage:@"1.jpg" DepartMent:@"销售部" Job:@"经理" Email:@"12345" Mobilephone:@"18720081687"];
    Contact *contact2 = [[Contact alloc]initWithId:@"3" Name:@"张2" HeadImage:@"hhh.png" DepartMent:@"销售部" Job:@"经理" Email:@"12345" Mobilephone:@"18720081687"];
    if(self.contactArray == nil){
        self.contactArray = [[NSMutableArray alloc]init];
    }
    [self.contactArray addObject:contact1];
    [self.contactArray addObject:contact2];
}

#pragma mark - 设置分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - 设置分区中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contactArray.count;
}

#pragma mark - 设置行显示的内容
- (ContactCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ContactCell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(self.cell==nil){
        self.cell = [[ContactCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    Contact *con = self.contactArray[indexPath.row];
    [self.cell setContact:con];
    self.cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return self.cell;
}

#pragma mark - 设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

#pragma mark - 设置允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark - 设置编辑类型
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.contactArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    [self.tableView reloadData];
}

#pragma mark - 设置允许移动行
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark - 移动行
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    Contact *tempCon = [self.contactArray objectAtIndex:fromIndexPath.row];
    [self.contactArray removeObjectAtIndex:fromIndexPath.row];
    [self.contactArray insertObject:tempCon atIndex:toIndexPath.row];
    [self.tableView reloadData];
}

#pragma mark - 选中某行查看详细资料
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"Contact2ContactDetail" sender:self.contactArray[indexPath.row]];
}

#pragma mark - 界面跳转
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"Contact2ContactDetail"]){
        NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow];
        ContactDetailViewController *cd =[[ContactDetailViewController alloc]init];
        cd = segue.destinationViewController ;
        [cd setContact:self.contactArray[indexPath.row]];
    }
}
@end
